package com.sanglh.tutorial.common.util;

import java.lang.reflect.Field;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionUtils {

	public static <T> void sort(ArrayList<T> entries, final String sortFieldName) {
		Collections.sort(entries, new Comparator<T>() {

			@Override
			public int compare(T lhs, T rhs) {
				try {
					Field lhsSortField = lhs.getClass().getField(sortFieldName);
					Field rhsSortField = rhs.getClass().getField(sortFieldName);
					Object lhsSortObject = lhsSortField.get(lhs);
					Object rhsSortObject = rhsSortField.get(rhs);
					return Collator.getInstance().compare(lhsSortObject,
							rhsSortObject);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 0;
			}
		});
	}
}
